package cn.maple.mq.Producer;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class Producer {


    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${rocketmq.producer.send-message-timeout}")  //读取配置文件 group
    private Integer messageTimeOut;

    /**
     * 同步消息
     *
     * @return
     */
    public SendResult sync() {
        SendResult result = rocketMQTemplate.syncSend("queue_test_topic", MessageBuilder.withPayload("同步消息").build());
        System.out.println(result.toString());
        return result;

    }

    /**
     * 异步消息
     */
    public void async() {
        rocketMQTemplate.asyncSend("queue_test_topic", MessageBuilder.withPayload("异步消息").build(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                // 处理消息发送成功逻辑
                System.out.println("异步消息=====");

            }

            @Override
            public void onException(Throwable e) {
                // 处理消息发送异常逻辑
                System.out.println("异步消息异常=====");
            }
        });

    }

    /**
     * 延时消息
     */
    public void delay() {
        SendResult sendResult = rocketMQTemplate.syncSend("queue_test_topic", MessageBuilder.withPayload("延迟消息").build(), messageTimeOut, 3);
        System.out.println("延时消息" + sendResult.toString());
    }

    /**
     * 单向消息
     */
    public void single() {
        rocketMQTemplate.sendOneWay("queue_test_topic", MessageBuilder.withPayload("+++单向消息").build());


    }

}
